<template>
    <div class="login"> 
<el-form :model="formLogin" ref="" :rules="rules" label-width="100px" class="demo-ruleForm login-form" >
            <h2>品优购后台管理-登录</h2>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="formLogin.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="formLogin.password"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
                <el-col :span="11"><el-input v-model="formLogin.code"></el-input></el-col>
                <el-col :span="2"><div style="margin:15px"></div></el-col>
                <el-col :span="11"><el-image :src="src" v-on:click="verifyCode()" style="width:100px;height:39px"></el-image></el-col>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" @click="login()">登录</el-button>
            </el-form-item>
        </el-form>
   </div>
</template>

<script>
// 引用 axios 
import axios from 'axios';
export default {
    name:'Login',
     data() {
      return {
        src: '',
        formLogin:{ // 表单数据对象
            username:'',
            password:'',
            code:'',
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '请输入验证码', trigger: 'blur' },
            { min: 4, max: 4, message: '长度在4个字符以内', trigger: 'blur' }
          ],
        }
      }
    },
    methods:{
        // 获取验证码
        verifyCode(){
            // 调用接口
            axios.get('http://adminapi.pyg.com/admin/captcha').then((res)=>{
                // console.log(res);
                let {data}=res.data;
                // console.log(data);
                this.src=data.src;
            })
        },
        login(){
            axios.post('http://adminapi.pyg.com/login',this.formLogin).then((res)=>{
                // console.log(res);
             var {code,data,msg}=res.data;
             if(code==200 && msg=='success'){
                 //成功
                    this.$message({
                      message: '登录成功',
                      type: 'success'
                     });
                     // 把用户名存储到前端
                     // 前端的存储 sessionStorage localStroage cookies 存，取，销毁
                     window.sessionStorage.setItem('name',data.username);
                     this.$router.push('/index');//跳转到首页
             }else{
                 this.$message.error(msg);
             }
            })
        }
    },
    mounted(){
        // 获取验证码
        this.verifyCode();
    }
}
</script>

<style>
    .login{
        background-color:aqua;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .login .login-form{
        width: 500px;
        background-color:wheat;
        padding: 30px;
        border-radius: 10px;
    }
   .login-form h2{
       text-align: center;
   }
 
</style>